package com.augurit.wf.bi.convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springside.modules.orm.Page;

import com.augurit.common.util.PageUtils;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;
import com.augurit.wf.bi.web.form.WfPageElementForm;
import com.augurit.wf.common.entity.WfActElemPrivDef;

public class WfActElemPrivDefConvertor {
	public static WfActElemPrivDefForm convertVoToForm(WfActElemPrivDef entity) {
		if(entity != null) {
			WfActElemPrivDefForm form = new WfActElemPrivDefForm();
			form.setId(entity.getId());
			form.setActivityName(entity.getActivityName());
			form.setElementId(entity.getElementId());
			form.setTemplateId(entity.getTemplateId());
			form.setVersion(entity.getVersion());
			form.setViewName(entity.getViewName());
			if(entity.getIsHidden() == null)
				form.setHidden(false);
			else
				form.setHidden(entity.getIsHidden().equals("1")?true:false);
			if(entity.getIsLock() == null)
				form.setLocked(false);
			else
				form.setLocked(entity.getIsLock().equals("1")?true:false);
			return form;
		}else
			return null;
	}
	
	public static void convertFormToVo(WfActElemPrivDefForm form, WfActElemPrivDef entity) {
		if(entity != null && form != null) {
			entity.setId(form.getId());
			if(form.getActivityName() != null && form.getActivityName().trim().length() > 0)
				entity.setActivityName(form.getActivityName().trim());
			if(form.getElementId() != null)
				entity.setElementId(form.getElementId());
			entity.setTemplateId(form.getTemplateId());
			
			entity.setIsHidden(form.isHidden()?WfPageElementForm.HIDDEN :WfPageElementForm.NOT_HIDDEN);
			entity.setIsLock(form.isLocked()?WfPageElementForm.LOCKED: WfPageElementForm.NOT_LOCKED);
			entity.setVersion(form.getVersion());
			entity.setViewName(form.getViewName());
		}
	}
	
	public static List<WfActElemPrivDefForm> convertVoListToFormList(List<WfActElemPrivDef> wfActElemPrivDefList) {
		if(wfActElemPrivDefList != null && wfActElemPrivDefList.size() > 0) {
			List<WfActElemPrivDefForm> wfActElemPrivDefFormList = new ArrayList();
			for(int i=0; i<wfActElemPrivDefList.size(); i++) {
				wfActElemPrivDefFormList.add(convertVoToForm(wfActElemPrivDefList.get(i)));
			}
			return wfActElemPrivDefFormList;
		}
		return null;
	}
	
	public static List<Map> convertVoListToMapList(List<WfActElemPrivDef> wfActElemPrivDefList) {
		if(wfActElemPrivDefList != null && wfActElemPrivDefList.size() > 0) {
			List<Map> mapList = new ArrayList();
			for(int i=0; i<wfActElemPrivDefList.size(); i++) {
				WfActElemPrivDef entity = wfActElemPrivDefList.get(i);
				Map<String, String> map = new HashMap();

				map.put("id", entity.getId() == null ? "" : entity.getId().toString());
				map.put("activityName", entity.getActivityName());
				map.put("elementId", entity.getElementId().toString());
				map.put("wfBusTemplateId", entity.getTemplateId() == null ? "" : entity.getTemplateId().toString());
				map.put("isHidden", entity.getIsHidden());
				map.put("isLock", entity.getIsLock());
				
				mapList.add(map);
			}
			return mapList;
		}
		return null;
	}
	
	public static List<WfActElemPrivDef> convertFormListToVoList(List<WfActElemPrivDefForm> wfActElemPrivDefFormList) {
		if(wfActElemPrivDefFormList != null && wfActElemPrivDefFormList.size() > 0) {
			List<WfActElemPrivDef> wfActElemPrivDefList = new ArrayList();
			for(int i=0; i<wfActElemPrivDefFormList.size(); i++) {
				WfActElemPrivDef wfActElemPrivDef = new WfActElemPrivDef();
				convertFormToVo(wfActElemPrivDefFormList.get(i), wfActElemPrivDef);
				wfActElemPrivDefList.add(wfActElemPrivDef);
			}
			return wfActElemPrivDefList;
		}
		return null;
	}
	
	public static List<WfActElemPrivDefForm> convertObjsListToFormList(List<Object []> objsList) {
		List<WfActElemPrivDefForm> result = null;
		if(objsList != null && objsList.size() > 0) {
			result = new ArrayList<WfActElemPrivDefForm>();
			for(Object [] objs:objsList) {
				WfActElemPrivDef entity = (WfActElemPrivDef)objs[0];
				String templateName = (String)objs[1];
				WfActElemPrivDefForm form = convertVoToForm(entity);
				result.add(form);
			}
		}
		return result;
	}
	
	/**
	 * 把WfPageElementForm、WfActElemPrivDefForm中的信息合并，因为WfPageElementForm中没有权限控制信息
	 * @param form 
	 * @param pageElemForm
	 * @return
	 */
	public static WfActElemPrivDefForm merge(WfActElemPrivDefForm form, WfPageElementForm pageElemForm){
		WfActElemPrivDefForm actElemPrivDefForm = new WfActElemPrivDefForm();
		actElemPrivDefForm.setId(pageElemForm.getId());
		actElemPrivDefForm.setElementId(pageElemForm.getId());
		actElemPrivDefForm.setElementCode(pageElemForm.getElementCode());
		actElemPrivDefForm.setElementName(pageElemForm.getElementName());
		actElemPrivDefForm.setTemplateId(form.getTemplateId());
		if(WfActElemPrivDefForm.DB_ZB_VIEW.equals(form.getViewName()))
			actElemPrivDefForm.setActivityName(form.getActivityName());
		else
			actElemPrivDefForm.setActivityName(null);
		actElemPrivDefForm.setVersion(form.getVersion());
		actElemPrivDefForm.setViewName(form.getViewName());
		return actElemPrivDefForm;
	}
	
	/**
	 * 把Page<WfPageElementForm>转化为Page<WfActElemPrivDefForm>
	 * @param pageElems
	 * @param elemPrivDef 页面元素权限控制信息
	 * @param form
	 * @return
	 */
	public static Page<WfActElemPrivDefForm> convertPageElemsToPageActElems(Page<WfPageElementForm> pageElems, List<WfActElemPrivDefForm> elemPrivDef, WfActElemPrivDefForm form){
		@SuppressWarnings(value={"unchecked"})
		Page<WfActElemPrivDefForm>  retPage = PageUtils.newPageInstance(pageElems);
		
		//设置权限信息
		if(pageElems.getResult() != null && pageElems.getResult().size() > 0){
			
			List<WfActElemPrivDefForm> result = new ArrayList<WfActElemPrivDefForm>();
			for(WfPageElementForm pageElemForm: pageElems.getResult()){
				//把WfPageElementForm转换为WfActElemPrivDefForm
				WfActElemPrivDefForm elemPrivDefForm = merge(form, pageElemForm);
				
				if(elemPrivDef != null && elemPrivDef.size() > 0){
					for(WfActElemPrivDefForm def:elemPrivDef){
						if(pageElemForm.getId().equals(def.getElementId())){
							elemPrivDefForm.setHidden(def.isHidden());
							elemPrivDefForm.setLocked(def.isLocked());
						}
					}
				}
				result.add(elemPrivDefForm);
			}
			
			retPage.setResult(result);
		}
		return retPage;
	}
}